﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace PreProcessingLanguage.Statistics
{
    class StatisticsProbability
    {
        Dictionary<string, Dictionary<string, double>> P = new Dictionary<string, Dictionary<string, double>>();
        Dictionary<string, double> p100 = new Dictionary<string, double>();
        Dictionary<string, double> p95 = new Dictionary<string, double>();
        Dictionary<string, double> p90 = new Dictionary<string, double>();
        Dictionary<string, double> p80 = new Dictionary<string, double>();
        Dictionary<string, double> p70 = new Dictionary<string, double>();
        public void readData(string path)
        {
            StreamReader sr = new StreamReader(path);
            Dictionary<string, double> pp;
            string str;
            while((str= sr.ReadLine())!=null)
            {
                string[] ppp=str.Split(':');
                string[] pppp = ppp[1].Split(';');
                pp = new Dictionary<string, double>();
                foreach (string s in pppp)
                {
                    string[] ppppp = s.Split('-');
                    pp.Add(ppppp[0],double.Parse(ppppp[1]));
                }
                P.Add(ppp[0], pp);
            }
            sr.Close();
        }
        public void statistic()
        {
            foreach (KeyValuePair<string,Dictionary<string,double>> pair in P)
            {
                foreach (KeyValuePair<string,double> pPair in pair.Value)
                {
                    if (pPair.Value == 100)
                        p100.Add(pPair.Key,pPair.Value);
                    if (pPair.Value > 95)
                        p95.Add(pPair.Key, pPair.Value);
                    if (pPair.Value > 90)
                        p90.Add(pPair.Key, pPair.Value);
                    if (pPair.Value > 80)
                        p80.Add(pPair.Key, pPair.Value);
                    if (pPair.Value > 70)
                        p70.Add(pPair.Key, pPair.Value);
                }
            }
        }
        public void writeData(string file1,string file2,string file3,string file4,string file5)
        {
            StreamWriter sw = new StreamWriter(file1);
            foreach (KeyValuePair<string,double> pair in p100)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Close();
            sw = new StreamWriter(file2);
            foreach (KeyValuePair<string, double> pair in p95)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Close();
            sw = new StreamWriter(file3);
            foreach (KeyValuePair<string, double> pair in p90)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Close();
            sw = new StreamWriter(file4);
            foreach (KeyValuePair<string, double> pair in p80)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Close();
            sw = new StreamWriter(file5);
            foreach (KeyValuePair<string, double> pair in p70)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Close();
        }
    }
}
